//
//  YHTopicViewController.m
//  FAKER
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHTopicViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "Focus.h"

@interface YHTopicViewController ()<YHScrollToTopDelegate, YHRemoveLoadingViewDelegate>
@property(nonatomic, strong)YHBottomView *bottomView;
@property(nonatomic, strong)YHSelectedTableView *topicTableView;
//@property(nonatomic, strong)UINavigationBar *yhNavigationBar;
//@property(nonatomic, strong)UINavigationItem *yhNavigationItem;
@property(nonatomic, strong)YHScrollToTopButtonView *scrollToTopButtonView;
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)YHLoadingView *loadingView;
@property(nonatomic, strong)UIView *navigationBarView;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *focusButton;
@end

@implementation YHTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.bounds = CGRectMake(0, 64, 0, 0);
    self.navigationController.navigationBar.hidden = 1;
    self.navigationController.automaticallyAdjustsScrollViewInsets = 0;
    self.automaticallyAdjustsScrollViewInsets = 0;
    _navigationBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [self.view addSubview:_navigationBarView];
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, SCREEN_WIDTH, 40)];
    _titleLabel.text = _topicName;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_navigationBarView addSubview:_titleLabel];
    
//    self.title = _topicName;
//    self.navigationItem.hidesBackButton = YES;
//    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    _manager = [CoreDataManager shareManager];
    
    //点击底部按钮方法
    [self clickBottomButtonAction];
    
    //创建返回顶部按钮
    [self createScrollToTopButtonView];
    
    //创建加载中视图
    _loadingView = [[YHLoadingView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 40)];
    [self.view addSubview:_loadingView];
}

#pragma mark - 移除加载中视图
- (void)yhRemoveLoadingView
{
    [_loadingView removeFromSuperview];
}

- (void)loadView
{
    [super loadView];
    
    //创建话题tableView
    _topicTableView = [[YHSelectedTableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 40) URLString:_urlString];
    _topicTableView.loadingDelegate = self;
    void (^scrollToTopBlock)(CGFloat) = ^(CGFloat y) {
        if (y > SCREEN_HEIGHT) {
            _scrollToTopButtonView.hidden = NO;
        }
        else if (y < SCREEN_HEIGHT) {
            _scrollToTopButtonView.hidden = YES;
        }
    };
    _topicTableView.scrollToTopBlock = scrollToTopBlock;
    [self.view addSubview:_topicTableView];
    
    //创建底部按钮
    _bottomView = [[YHBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    [self.view addSubview:_bottomView];
}

//#pragma mark - 创建navigationBar
//- (void)createNavigationBar
//{
    //创建navigationBar
//    _yhNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
//    //修改navigationBar
//    _yhNavigationBar.barTintColor = [UIColor colorWithRed:0.57 green:0.79 blue:0.66 alpha:1];
//    [_yhNavigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
//    
//    //创建navigationItem
//    _yhNavigationItem = [[UINavigationItem alloc] initWithTitle:_topicName];
//    //在navigationBar上显示navigationItem
//    [_yhNavigationBar pushNavigationItem:_yhNavigationItem animated:YES];
//    [self.view addSubview:_yhNavigationBar];
//}

#pragma mark - 点击方法
#pragma mark 点击关注按钮
- (void)clickAttentionButtonAction
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"focus"];
    
    //取消关注
    NSArray *resultArray = [self searchCoreData];
    for (Focus *focus in resultArray) {
        if ([focus.title isEqualToString:_topicName]) {
//            self.navigationItem.rightBarButtonItem.title = @"关注";
            [_focusButton setTitle:@"关注" forState:UIControlStateNormal];
            [_manager.managedObjectContext deleteObject:focus];
            [_manager saveContext];
            return;
        }
    }
    
    //添加关注
//    self.navigationItem.rightBarButtonItem.title = @"已关注";
    [_focusButton setTitle:@"已关注" forState:UIControlStateNormal];
    Focus *focus = [NSEntityDescription insertNewObjectForEntityForName:@"Focus" inManagedObjectContext:_manager.managedObjectContext];
    focus.title = _topicName;
    focus.api_url = _urlString;
    focus.pic = _coverImageURL;
    [_manager saveContext];
}
#pragma mark 点击底部按钮
- (void)clickBottomButtonAction
{
    void (^bottomButtonBlock)(NSString *) = ^(NSString *buttonTitle) {
        if ([buttonTitle isEqualToString:@"back"]) {
//            [self dismissViewControllerAnimated:YES completion:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            _scrollToTopButtonView.hidden = YES;
            _topicTableView.startRefresh = YES;
        }
    };
    _bottomView.block = bottomButtonBlock;
}

#pragma mark - 查询数据库
- (NSArray *)searchCoreData
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Focus"];
    return [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
}

#pragma mark - 创建返回顶部按钮
- (void)createScrollToTopButtonView
{
    _scrollToTopButtonView = [[YHScrollToTopButtonView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 45, SCREEN_HEIGHT - 40 - 45, 35, 35)];
    _scrollToTopButtonView.delegate = self;
    _scrollToTopButtonView.hidden = YES;
    [self.view addSubview:_scrollToTopButtonView];
}
#pragma mark 返回顶部协议方法
- (void)yhScrollToTop
{
    [_topicTableView.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark - 视图即将出现判断是否已经关注并创建
- (void)viewWillAppear:(BOOL)animated
{
    _focusButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _focusButton.frame = CGRectMake(_titleLabel.WIDTH - 60 - 10, _titleLabel.Y, 60, _titleLabel.HEIGHT);
//    _focusButton.backgroundColor = [UIColor blueColor];
    _focusButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    _focusButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    [_focusButton addTarget:self action:@selector(clickAttentionButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_focusButton];
    
    //创建关注按钮
    NSArray *resultArray = [self searchCoreData];
    for (Focus *focus in resultArray) {
        if ([focus.title isEqualToString:_topicName]) {
//            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已关注" style:UIBarButtonItemStylePlain target:self action:@selector(clickAttentionButtonAction)];
            [_focusButton setTitle:@"已关注" forState:UIControlStateNormal];
            [self changeRightBarButtonItemTitleColor];
            return;
        }
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关注" style:UIBarButtonItemStylePlain target:self action:@selector(clickAttentionButtonAction)];
    [_focusButton setTitle:@"关注" forState:UIControlStateNormal];
    [self changeRightBarButtonItemTitleColor];
    
//    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10], NSFontAttributeName, nil] forState:UIControlStateNormal];
}

- (void)changeRightBarButtonItemTitleColor
{
    UIColor *currentColor = [UIColor whiteColor];
    if (NSUSERDEFAULTS_NIGHT) {
        currentColor = [UIColor lightGrayColor];
        _navigationBarView.backgroundColor = BGCOLOR_BLACK;
        _titleLabel.textColor = [UIColor lightGrayColor];
        [_focusButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    } else {
        _navigationBarView.backgroundColor = BGCOLOR_RED;
        _titleLabel.textColor = [UIColor whiteColor];
        [_focusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:currentColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
